<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        .login-container {
            background-color: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            text-align: center;
        }

        .login-header {
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: bold;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
        }

        .captcha-group {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .captcha-group input {
            flex: 1;
        }

        .captcha-image {
            width: 120px;
            height: 40px;
            cursor: pointer;
            border-radius: 5px;
            border: 1px solid #ddd;
        }

        .error-message {
            background-color: #fee;
            color: #d32f2f;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .login-button {
            width: 100%;
            padding: 12px;
            background-color: #667eea;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .login-button:hover {
            background-color: #5a67d8;
        }

        .auto-login-button {
            width: 100%;
            padding: 12px;
            background-color: #48bb78;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 10px;
        }

        .auto-login-button:hover {
            background-color: #38a169;
        }

        .login-footer {
            margin-top: 20px;
            font-size: 14px;
            color: #666;
        }

        .login-footer a {
            color: #667eea;
            text-decoration: none;
        }

        .login-footer a:hover {
            text-decoration: underline;
        }

        .register-link {
            margin-top: 20px;
            font-size: 14px;
            color: #666;
        }

        .register-link a {
            color: #667eea;
            text-decoration: none;
        }

        .register-link a:hover {
            text-decoration: underline;
        }

        @media (max-width: 480px) {
            .login-container {
                padding: 20px;
                margin: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1>欢迎登录</h1>
            <p>请输入您的账号和密码</p>
        </div>

        <form th:action="@{/login}" method="post" id="loginForm">
            <input type="hidden" th:name="captchaCode" th:value="${captchaCode}" id="captchaCodeHidden">
            
            <div th:if="${error}" class="error-message" th:text="${error}"></div>

            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" placeholder="请输入用户名" required>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required>
            </div>

            <div class="form-group">
                <label for="captcha">验证码</label>
                <div class="captcha-group">
                    <input type="text" id="captcha" name="captcha" placeholder="请输入验证码" required>
                    <img id="captchaImage" class="captcha-image" th:src="|data:image/jpeg;base64,${captchaBase64}|" alt="验证码" onclick="refreshCaptcha()">
                </div>
            </div>

            <button type="submit" class="login-button">登录</button>
            <button type="button" class="auto-login-button" onclick="autoLoginAsTestAdmin()">自动以测试管理员身份登录</button>
            
            <!-- 注册链接 -->
            <div class="register-link">
                没有账号？<a th:href="@{/register}">立即注册</a>
            </div>
        </form>

        <div class="login-footer">
            <a href="#">忘记密码?</a>
        </div>
        
        <div style="margin-top: 20px; padding: 15px; background-color: #f8f9fa; border-radius: 5px; font-size: 14px;">
            <p style="margin: 0; color: #666;">默认账号信息：</p>
            <p style="margin: 5px 0 0; color: #666;">用户名：testadmin 或 admin</p>
            <p style="margin: 0; color: #666;">密码：123456 或 admin123</p>
        </div>
    </div>

    <script>
        function refreshCaptcha() {
            fetch('/captcha')
                .then(response => response.text())
                .then(data => {
                    document.getElementById('captchaImage').src = 'data:image/jpeg;base64,' + data;
                })
                .catch(error => {
                    console.error('刷新验证码失败:', error);
                });
        }

        function autoLoginAsTestAdmin() {
            // 自动填充测试管理员账号信息
            document.getElementById('username').value = 'testadmin';
            document.getElementById('password').value = '123456';
            
            // 使用隐藏字段中的验证码值
            const captchaCode = document.getElementById('captchaCodeHidden').value;
            document.getElementById('captcha').value = captchaCode;
            
            // 提交表单
            document.getElementById('loginForm').submit();
        }
    </script>
</body>
</html>